{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 在Keras中使用圖像增強來進行深度學習\n",
    "\n",
    "使用神經網絡和深度學習模型時需要準備數據。對於複雜的圖像對象物件識別的任務，也需要越來越多的數據增強功能。\n",
    "\n",
    "在這篇文章中，您將了解使用Keras開發深度學習模型時，如何使用數據準備和數據增強來讓您的模型的效能更好。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Keras圖像增強API\n",
    "\n",
    "\n",
    "Keras的圖像增強(image augmentation) API簡單而強大。\n",
    "\n",
    "\n",
    "Keras提供ImageDataGenerator類別，用於定義圖像數據準備和增強的配置。這包括以下功能：\n",
    "\n",
    "* 圖像隨機旋轉(rotate)\n",
    "* 圖像隨機偏移(shift)\n",
    "* 圖像隨機推移錯切(shear)\n",
    "* 圖像隨機翻轉(flip)\n",
    "* Sample-wise 圖像像素標準化\n",
    "* Feature-wise 圖像像素標準化\n",
    "* ZCA 白化轉換\n",
    "* 圖像張量維度的重排序\n",
    "* 儲存增強圖像數據\n",
    "\n",
    "增強圖像生成器可以透過以下的方法創建：\n",
    "\n",
    "    datagen = ImageDataGenerator()\n",
    "    \n",
    "Keras並不是在記憶體中對整個圖像數據集執行圖像轉換操作，而是設計為通過深度學習模型訓練過程進行迭代，從而為您動態地創建增強的圖像數據。這會減少您的記憶體開銷，但在模型訓練期間會增加一些額外的時間成本。\n",
    "\n",
    "創建並配置好**ImageDataGenerator**之後，您必須用您的數據來訓練它。這個步驟將計算實際執行轉換到您的圖像數據所需的任何統計參數。您可以通過調用數據生成器上的**fit( )**函數並將傳遞您的訓練數據集來完成這個前置動作。\n",
    "\n",
    "    datagen.fit(train)\n",
    "    \n",
    "數據生成器本身實際上是一個迭代器，當被呼叫時返回一個批量的圖像資料。我們可以通過調用**flow( )**函數來配置批量大小並並獲取批量的圖像資料。\n",
    "    \n",
    "    X_batch, y_batch = datagen.flow(train, train, batch_size=32)\n",
    "\n",
    "最後我們就可以使用數據生成器。我們不必調用我們模型的**fit( )**函數，而是調用**fit_generator( )**函數，並傳遞數據生成器的實例(instance)和每個循環的步數(steps_per_epoch)以及要訓練的循環總數(epochs)。\n",
    "\n",
    "    model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),\n",
    "                        steps_per_epoch=len(X_train)/batch_size,\n",
    "                        epochs=nb_epoch)\n",
    "                        \n",
    "您可以在Keras文檔中了解關於Keras[圖像數據生成器API](http://keras.io/preprocessing/image/)或是[圖像數據生成器API中文](http://eighty20.cc/keras-docs-tw/preprocessing/image/)更多信息。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 圖像增強(Image Augmentation)相關參數的比較\n",
    "\n",
    "現在您已經知道Keras中的圖像增強API是如何工作的，我們來看一些例子。\n",
    "\n",
    "這些例子中我們將使用MNIST手寫數字識別的圖像集。首先，讓我們看看訓練數據集中的前6個圖像。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFICAYAAACImy0WAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAGy1JREFUeJzt3XmQFfW1wPHzgwFkEQgJQaLFJosL\nwggiQlFAIqAiUcCIIjKBRLEkqLGEwpjRDCoguLxCBOOTsFNBKiiokRIjCJFlCqKYUjIKGNkFVAZm\nWIYH9PsDXtX79Wlzh8u999zl+6miKufUud1H0jOHnv7Nr10QBAIAAFKrinUDAADkIgYwAAAGGMAA\nABhgAAMAYIABDACAAQYwAAAGGMAAABhgAKeAc+4D59xx51z52T+fW/eE7Oaca+Cce8M5d8Q5t905\nd5d1T8gdzrlWZ7/nzbfuJZ0xgFNnVBAEdc7+aWPdDLLeNBE5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5+OeZwgCBLVEgCI\niEizZs1ULup7VJcuXby4bdu2cZ2vcePGXvzggw/GdZx0wB0wAAAGGMAAABhgAAMAYIABDACAARZh\nZYCmTZt68QUXXGDUCdJN586dvfjuu+9WNT169FC5K6+8MuaxR48e7cV79uxRNd26dfPi+fPnq5ri\n4uKY50J6uuyyy1Tut7/9rRcPGTJE1dSsWVPlnHNevHPnTlVTVlbmxZdffrmqGTRokBdPnz5d1ZSU\nlKhcOuIOGAAAAwxgAAAMMIABADDAAAYAwACLsNJMr169VO6BBx6I+bnwooN+/fqpmn379sXfGMzd\ncccdKjdlyhQv/tGPfqRqwotfREQ++OADL45668yzzz4bs6fwsaOOc+edd8Y8DlKvXr16Kjdp0iQv\njrrmwm81qqwtW7Z48Q033KBqqlWr5sVRi6nC13jUNZ8puAMGAMAAAxgAAAMMYAAADPAM2Fh4I4NZ\ns2apmqhnNWHh53Xbt28/v8aQUnl5+kvxmmuu8eJXX31V1dSqVcuLV69erWqeeuoplfvwww+9uEaN\nGqpm0aJFXtynTx9VE7Zx48aYNUgPAwYMULl77rknIcfetm2byvXu3duLozbiaNmyZULOnym4AwYA\nwAADGAAAAwxgAAAMMIABADDAIixjv/zlL734Jz/5SczPhDdREBGZO3duolqCgai3GM2YMSPm5957\n7z0vjto44fDhwzGPE/W5yiy62rVrlxfPmTMn5meQHm6//fa4PvfVV1958YYNG1TN2LFjVS5q0VVY\n1NuPshl3wAAAGGAAAwBggAEMAIABngGnUNSm4b/61a+8+PTp06qmtLTUi59++unENoaUC2+O8dhj\nj6maIAi8ePr06aqmsLDQiyvzvDfK73//+7g+9+CDD3rxgQMH4joOUu/ee+9VuREjRnjx8uXLVc3W\nrVu9eP/+/QnrqVGjRgk7VibgDhgAAAMMYAAADDCAAQAwwAAGAMAAi7CSpFmzZiq3ePHiuI41depU\nL165cmVcx4GNJ554QuXCi65OnDihat59910vjtrc4NixYzHPf8EFF6hceJONJk2aqBrnnBdHLf5b\nunRpzPMjPe3Zs0flioqKUt/I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kof4YVZ\nURtx3H///V7crl07VZMJm3OwCAupxiIsAADSGAMYAAADDGAAAAzwDBgpwzNgWOAZMFKNZ8AAAKQx\nBjAAAAYYwAAAGGAAAwBgIKWLsAAAwBncAQMAYIABDACAAQYwAAAGGMAAABhgAAMAYIABDACAAQYw\nAAAGGMAAABhgAAMAYIABDACAAQYwAAAGGMAAABhgAAMAYIABDACAAQYwAAAGGMAAABhgAAMAYIAB\nDACAAQYwAAAGGMAAABhgAAMAYIABDACAAQYwAAAG/hfHno9VjYUCHwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1765c7fe0f0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import keras\n",
    "# Plot images\n",
    "from keras.datasets import mnist\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# load data\n",
    "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
    "\n",
    "plt.figure(figsize=(8,8)) # 設定每個圖像顯示的大小\n",
    "\n",
    "# 產生一個3x2網格的組合圖像\n",
    "for i in range(0, 6):\n",
    "    plt.subplot(330+1+i) # (331) -> 第一個子圖像, (332) -> 第二個子圖像\n",
    "    plt.title(y_train[i])\n",
    "    plt.axis('off')     #不顯示坐標\n",
    "    plt.imshow(X_train[i], cmap=plt.get_cmap('gray')) # 以灰階的圖像顯示\n",
    "    \n",
    "# 展現出圖像\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "運行這個例子會提供了以上的圖片，我們可以用它來比較下面例子中的圖片準備(image preparation)和增強(augmentation)對圖像轉換效果的理解。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 隨機旋轉 (Random Rotations)\n",
    "\n",
    "有時您的樣本數據中的圖像可能在場景中會有不同的旋轉的角度。\n",
    "\n",
    "在訓練您的模型時，妳可以透過隨機旋轉訓練數據集裡的圖像來產生一些新的訓練圖像來幫助您的模型可以更好地處理圖像的旋轉問題。\n",
    "\n",
    "下面的範例通過設置rotation_range參數來創建更多的MNIST數字圖像(隨機旋轉最多可達90度)。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFICAYAAACImy0WAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAHDlJREFUeJzt3XtwVdX1wPG1CSAvA0FARUHkKdAK\ng9haAaVaQER/kGJH5SUqozwc5TeiQgFFFMGCOFp+PGYc0eIUpZUig0V5VBEUFGSQCiYZigVSIwTK\nSxCU5Pz+iB3dZx96Tq733nVy7/cz05muNeues4WbrFz2yj7G8zwBAADpVU17AQAAZCMaMAAACmjA\nAAAooAEDAKCABgwAgAIaMAAACmjAAAAooAGnmDHmPmPMFmPMaWPMS9rrQXYwxjQ0xvzFGHPCGLPH\nGDNIe03IfMaYV4wxJcaYY8aYImPMCO01xZnhII7UMsb8WkTKRaSPiNT2PG+47oqQDYwxi6XiB+y7\nRaSziLwpIld7nrdDdWHIaMaYjiKyy/O808aYy0TkXRHp53nex7oriyc+AaeY53lLPc9bJiKHtNeC\n7GCMqSsiA0Vksud5X3met0FElovIUN2VIdN5nrfD87zT/wm/+18rxSXFGg0YyDxtRaTM87yiH+Q+\nEZGOSutBFjHGzDXGnBSRAhEpEZG/Ki8ptmjAQOapJyJHfbmjInKuwlqQZTzPGy0V77UeIrJURE7/\n91dkLxowkHm+EpFcXy5XRI4rrAVZyPO8su+2Pi4WkVHa64krGjCQeYpEpLoxps0Pcp1EhAEspFt1\nYQ/4rGjAKWaMqW6MqSUiOSKSY4ypZYyprr0uZC7P805IxT/9TTXG1DXGdBOR/iKySHdlyGTGmCbG\nmNuMMfWMMTnGmD4ic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ABAFBAAwYAQAENGAAABTRgAAAU0IABAFBAAwYAQAENGAAABTRgAAAU0IABAFBAAwYAQEFa\nn4YEAAAq8AkYAAAFNGAAABTQgAEAUEADBgBAAQ0YAAAFNGAAABTQgAEAUEADBgBAAQ0YAAAFNGAA\nABTQgAEAUEADBgBAAQ0YAAAFNGAAABTQgAEAUEADBgBAAQ0YAAAFNGAAABTQgAEAUEADBgBAAQ0Y\nAAAFNGAAABTQgAEAUPD/yjA2nuaVEgoAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1765c7fe6d8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 隨機旋轉 (Random Rotations)\n",
    "from keras.datasets import mnist\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 載入數據\n",
    "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
    "\n",
    "# 將圖像數據集的維度進行改變 \n",
    "# 改變前: [樣本數, 圖像寬, 圖像高] -> 改變後: [樣本數, 圖像寬, 圖像高, 圖像頻道數]\n",
    "X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\n",
    "X_test  = X_test.reshape(X_test.shape[0], 28, 28, 1)\n",
    "\n",
    "# 將像素值由\"整數(0~255)\"換成\"浮點數(0.0~255.0)\"\n",
    "X_train = X_train.astype('float32')\n",
    "X_test = X_test.astype('float32')\n",
    "\n",
    "# 定義\"圖像數據增強產生器(ImageDataGenerator)\"的參數\n",
    "datagen = ImageDataGenerator(rotation_range=90)\n",
    "\n",
    "# 透過訓練數據集來訓練(fit)圖像數據增強產生器(ImageDataGenerator)的實例\n",
    "datagen.fit(X_train)\n",
    "\n",
    "# 設定要\"圖像數據增強產生器(ImageDataGenerator)\"產生的圖像批次值(batch size)\n",
    "# \"圖像數據增強產生器(ImageDataGenerator)\"會根據設定回傳指定批次量的新生成圖像數據\n",
    "for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9):\n",
    "    plt.figure(figsize=(8,8)) # 設定每個圖像顯示的大小\n",
    "    # 產生一個3x3網格的組合圖像\n",
    "    for i in range(0, 6):\n",
    "        plt.subplot(331+i)\n",
    "        plt.title(y_batch[i]) # 秀出圖像的真實值\n",
    "        plt.axis('off')     #不顯示坐標\n",
    "        plt.imshow(X_batch[i].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
    "        \n",
    "    plt.show()\n",
    "    break # 跳出迴圈"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "運行以上範例，您可以看到圖像已左右旋轉至90度的極限。這個\"隨機旋轉\"的功能對解決MNIST數字辨識這個問題沒有什麼幫助，因為MNIST數字有一個標準化的方向，但是當您要辨識的對象或物件需要不同方向的照片來進行訓練學習時，這種轉換可能有相當的幫助。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 隨機偏移 (Random Shifts)\n",
    "\n",
    "圖像中的對象或物件在圖框中的位置可能在最中間。他們可能以各種不同的方式偏離圖框的中心點。\n",
    "\n",
    "您可以訓練您的深度學習網絡，通過人工創建您的訓練數據的偏移圖像來讓您的模型能夠辨識和處理偏離中心的對象或物件。 Keras的\"圖像數據增強產生器(ImageDataGenerator)\"支持通過width_shift_range和height_shift_range兩個參數設定來對訓練數據進行水平和垂直隨機轉換來產生新圖像數據。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFICAYAAACImy0WAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAHYtJREFUeJzt3Xu0zXX6wPHnQwflSDFRIZfk10Wj\nCLN0XGahyxGGmqnpoqIxIxNjMRkNFiY1p3LN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7iAIAs1aJFC5VLZtPV8uXLVW706NFp6QnpwxMwAAAGGMAAABhg\nAAMAYIA1YADIEgUFBV48c+bMlK4zf/58lQu/jAH2eAIGAMAAAxgAAAMMYAAADDCAAQAwEOvbkAAA\nwHE8AQMAYIABDACAAQYwAAAGGMAAABhgAAMAYIABDACAAQYwAAAGGMAAABhgAAMAYIABDACAAQYw\nAAAGGMAAABhgAAMAYIABDACAAQYwAAAGGMAAABhgAAMAYIABDACAAQYwAAAGGMAAABhgAAMAYIAB\nDACAAQYwAAAG/g+VW4WxX8adpwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1765e0da160>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 隨機偏移 (Random Shifts)\n",
    "from keras.datasets import mnist\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 載入數據\n",
    "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
    "\n",
    "# 將圖像數據集的維度進行改變 \n",
    "# 改變前: [樣本數, 圖像寬, 圖像高] -> 改變後: [樣本數, 圖像寬, 圖像高, 圖像頻道數]\n",
    "X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\n",
    "X_test  = X_test.reshape(X_test.shape[0], 28, 28, 1)\n",
    "\n",
    "# 將像素值由\"整數(0~255)\"換成\"浮點數(0.0~255.0)\"\n",
    "X_train = X_train.astype('float32')\n",
    "X_test = X_test.astype('float32')\n",
    "\n",
    "# 定義\"圖像數據增強產生器(ImageDataGenerator)\"的參數\n",
    "shift = 0.2\n",
    "datagen = ImageDataGenerator(width_shift_range=shift, height_shift_range=shift)\n",
    "\n",
    "# 透過訓練數據集來訓練(fit)圖像數據增強產生器(ImageDataGenerator)的實例\n",
    "datagen.fit(X_train)\n",
    "\n",
    "# 設定要\"圖像數據增強產生器(ImageDataGenerator)\"產生的圖像批次值(batch size)\n",
    "# \"圖像數據增強產生器(ImageDataGenerator)\"會根據設定回傳指定批次量的新生成圖像數據\n",
    "for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9):\n",
    "    plt.figure(figsize=(8,8)) # 設定每個圖像顯示的大小\n",
    "    # 產生一個3x3網格的組合圖像\n",
    "    for i in range(0, 6):\n",
    "        plt.subplot(331+i)\n",
    "        plt.title(y_batch[i]) # 秀出圖像的真實值\n",
    "        plt.axis('off')     #不顯示坐標\n",
    "        plt.imshow(X_batch[i].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
    "        \n",
    "    plt.show()\n",
    "    break # 跳出迴圈"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "運行此範例將創建新的MNIST數字圖像的位移版本。同樣的，這對於MNIST來說不是必須的，因為手寫數字已經居中，但是你可以看到這對於更複雜的圖像問題來說可能是有用的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 隨機推移錯切 (Random Shear)\n",
    "\n",
    "透過一個圖像的錯切變換，圖像以它的中心垂直軸不變動的方式變形。可以用這樣的技術來產生新的經過錯切變換的圖像數據。\n",
    "\n",
    "詳細說明見:[Wikiepedia-圖像錯切](https://zh.wikipedia.org/wiki/%E9%94%99%E5%88%87)\n",
    "\n",
    "![Wikipedia-圖像錯切](https://upload.wikimedia.org/wikipedia/commons/thumb/0/08/Eigen.jpg/330px-Eigen.jpg)\n",
    "\n",
    "\n",
    "Keras的\"圖像數據增強產生器(ImageDataGenerator)\"支持使用shear_range參數設定來對訓練數據進行圖像錯切變換來產生新圖像數據。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFICAYAAACImy0WAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vb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lat6o11797dir/61a96OdOmTbPigQMHejkDBgyw4iZNmng5IR9+\n+KEVhzaO2Lt3b6Jrxcn3NeBRo0Z5Y6EelCS9K+731q9+9Ssv584777Ti0OYroc1nRo4cacVLly71\ncnr37m3F/fr183K6du3qjaUNa8AAAKQYBRgAAAUUYAAAFFCAAQBQUCGasNy/7P/3v//dy+nSpYsV\nuxsdiISbWLZt22bFoRNC3FNEhg8f7uXMmzfPijP9dV25cqU3duLECSt2m2NERI4fP/65cRrkYxNW\nEqEmFbf57u23387o2m5DXmgjkFDToHuize7du72cli1bWnHNmjVj5xPadMS9l4jfkLNp06bYa2cq\n35qw3I03nn32WS/n1ltv9cb+/Oc/W7HbwCciMnPmzM+NRUSOHj1qxaHvo86dO3tjnTp1suJPP/3U\nywk1DLrczWfOnTsXm5NrNGEBAJBiFGAAABRQgAEAUEABBgBAQRXtCSThNl25TVkiIs2aNYu9zujR\no72x6dOnW/H48eNjr+M2XImI/OAHP7DiuXPnxl4nxN0JRiTZDjatWrWy4l69enk5RUVF3tiOHTus\nOEkTGs5PqEnEbXq66aabYq+zevVqb8w9qeuRRx7xctzmm9C1PvjgAy/HbYy6+uqrvRx3R6tx48Z5\nOfv37/fGtm7d6o0hmTp16lix24AqEm4CdXdtCz0XDz74oBVXqeKXiKFDh1px6JS30KlK7pySNFyF\nuD9Pof/WUOOhu2vbL3/5y4zun028AQMAoIACDACAAgowAAAKKsQasCu0EceYMWOsOHTyUGgNy13z\nbdy4cWxOSGhd2JXp5hzu526//fbYz6xatcob++IXv+iN9enTx4pDf/He5W4iISIydepUKw6dboPP\ntmLFiqxc56GHHsrocwsXLvTGTp8+Hfu5OXPmWPFrr73m5SQ5PQzJDRkyxIovueSSRJ9zv9sOHDjg\n5fTv39+KFy9e7OXccMMNVhxa79UW+q5t06aNFbMGDABAgaIAAwCggAIMAIACCjAAAAoqxGlImQj9\nRX/35BgRkQ4dOljx9u3bvZyuXbvGXqd9+/ZWHPp1DW2o4Z4UU716dS8nCffEndD9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+sUaNGsZ87deqUFVevXt3LcZtmRETO\nnTtnxU8++aSX8/TTT1vx4MGDvZxnnnnGig8dOvTZk1Wi8dyV5zN3xx13WPHPfvYzLye065W7a5kb\niyRrznNVqsR7nCvpM8evHAAACijAAAAooAADAKCAjTgc2VrvDW2e4a73ftYYCk9oA4skmymE1nxd\n7nqviMj06dOt+PXXX/dy9u7da8WLFi3yctK45pvvRo8ebcVVq1ZN9Lkk67tuzsGDB72cBx54INH9\nEI83YAAAFFCAAQBQQAEGAEABBRgAAAV5uxHH5MmTvbFsnUZEM1Vm2Ijjwhw7dswbq1u3rhV/+OGH\nXs6qVau8sdDpS64zZ86cx+zSK9824ti3b58VN2nSJNHnNm/ebMWhjTjeeustK545c6aXs3///kT3\nK2RsxAEAQIpRgAEAUEABBgBAQd6uAYckWRcOrYsgO1gDzj53Xfjb3/62l1NaWuqNbd++vdzmlDb5\ntgZ88803W3GLFi28nFatWnljd999d3lNCQ7WgAEASDEKMAAACijAAAAooAADAKCgoJqwoIsmLGjI\ntyYspB9NWAAApBgFGAAABRRgAAAUUIABAFBAAQYAQAEFGAAABRRgAAAUUIABAFCQ0404AABAGd6A\nAQBQQAEGAEABBRgAAAUUYAAAFFCAAQBQQAEGAEABBRgAAAUUYAAAFFCAAQBQQAEGAEABBRgAAAUU\nYAAAFFCAAQBQQAEGAEABBRgAAAUUYAAAFFCAAQBQQAEGAEABBRgAAAUUYAAAFFCAAQBQQAEGAEAB\nBRgAAAX/HzBl0gawT9bXAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1765c2baf98>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 隨機推移錯切 (Random Shear)\n",
    "from keras.datasets import mnist\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 載入數據\n",
    "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
    "\n",
    "# 將圖像數據集的維度進行改變 \n",
    "# 改變前: [樣本數, 圖像寬, 圖像高] -> 改變後: [樣本數, 圖像寬, 圖像高, 圖像頻道數]\n",
    "X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\n",
    "X_test  = X_test.reshape(X_test.shape[0], 28, 28, 1)\n",
    "\n",
    "# 將像素值由\"整數(0~255)\"換成\"浮點數(0.0~255.0)\"\n",
    "X_train = X_train.astype('float32')\n",
    "X_test = X_test.astype('float32')\n",
    "\n",
    "# 定義\"圖像數據增強產生器(ImageDataGenerator)\"的參數\n",
    "shear_range=1.25 # 推移錯切的強度\n",
    "datagen = ImageDataGenerator(shear_range=shear_range)\n",
    "\n",
    "# 透過訓練數據集來訓練(fit)圖像數據增強產生器(ImageDataGenerator)的實例\n",
    "datagen.fit(X_train)\n",
    "\n",
    "# 設定要\"圖像數據增強產生器(ImageDataGenerator)\"產生的圖像批次值(batch size)\n",
    "# \"圖像數據增強產生器(ImageDataGenerator)\"會根據設定回傳指定批次量的新生成圖像數據\n",
    "for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9):\n",
    "    plt.figure(figsize=(8,8)) # 設定每個圖像顯示的大小\n",
    "    # 產生一個3x3網格的組合圖像\n",
    "    for i in range(0, 6):\n",
    "        plt.subplot(331+i)\n",
    "        plt.title(y_batch[i]) # 秀出圖像的真實值\n",
    "        plt.axis('off')     #不顯示坐標\n",
    "        plt.imshow(X_batch[i].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
    "        \n",
    "    plt.show()\n",
    "    break # 跳出迴圈"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 隨機鏡像翻轉 (Random Flips)\n",
    "\n",
    "另一個可以提高大型複雜圖像問題辨識性能的圖像增強法，是在您的圖像訓練數據中利用圖像隨機翻轉來產生新的圖像數據。\n",
    "\n",
    "Keras的\"圖像數據增強產生器(ImageDataGenerator)\"支持使用vertical_flip和horizontal_flip兩個參數設定來對訓練數據進行垂直和水平軸隨機翻轉來產生新圖像數據。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFICAYAAACImy0WAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAHIJJREFUeJzt3XmQVcX1wPHTMKLsqIABVAg/JAqi\nExQFLSEpkGVAUNDITxMI0SJICe4WBJIQ16BSvyA6aFREHJCwqcgmIdGoaAoIKkTQkQgqy7AvgrJO\n//4YU2V3X32P53v3vHnv+6nyj3PqvHs7k8s7XLqn21hrBQAAxKuK9gAAAMhHNGAAABTQgAEAUEAD\nBgBAAQ0YAAAFNGAAABTQgAEAUEADjoExppkxZoExZrcxpswY85gxpkB7XMhdxphzjDF/N8bsNcas\nM8ZcpT0m5D5jTIkxZosxZp8xptQYc6P2mLIZDTgexSKyTUQaiUihiHQSkaGqI0LO+vovdy+LyDwR\nOUVEBotIiTGmperAkA8eFJFm1to6ItJbRO4zxlygPKasRQOOxw9FZIa19qC1tkxEFolIa+UxIXed\nLSKNReT/rLXHrLV/F5GlIvIL3WEh11lrP7DWHvpv+PV//6M4pKxGA47HeBHpb4ypYYxpIiI9pKIJ\nA5lgviV3btwDQf4xxhQbY74UkQ9FZIuILFAeUtaiAcfjH1LxxrtPRDaKyAoReUl1RMhlH0rFlMdd\nxpgTjDFdpWLao4busJAPrLVDRaS2iFwmInNE5NB3fyJ/0YAzzBhTRURelYoHsaaI1BeRk0VkrOa4\nkLustUdE5EoR6SkiZSJyh4jMkIq//AEZ9/XUx1sicrqI3KQ9nmxFA868U0TkDBF5zFp7yFq7U0Se\nFZEi3WEhl1lrV1lrO1lrT7XWdhOR5iKyTHtcyDsFwhzwt6IBZ5i1doeIrBeRm4wxBcaYe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Igbc8AAAGQx\nGjAAAApowAAAKKABAwCgIK8WYUEXi7CggUVYiBuLsAAAyGI0YAAAFNCAAQBQQAMGAEABDRgAAAU0\nYAAAFNCAAQBQQAMGAEBBrBtxAACACrwBAwCggAYMAIACGjAAAApowAAAKKABAwCggAYMAIACGjAA\nAApowAAAKKABAwCggAYMAIACGjAAAApowAAAKKABAwCggAYMAIACGjAAAApowAAAKKABAwCggAYM\nAIACGjAAAApowAAAKKABAwCggAYMAIACGjAAAAr+HyR3rPwvqlh7AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1765c01e588>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 隨機鏡像翻轉 (Random Flips)\n",
    "from keras.datasets import mnist\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 載入數據\n",
    "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
    "\n",
    "# 將圖像數據集的維度進行改變 \n",
    "# 改變前: [樣本數, 圖像寬, 圖像高] -> 改變後: [樣本數, 圖像寬, 圖像高, 圖像頻道數]\n",
    "X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\n",
    "X_test  = X_test.reshape(X_test.shape[0], 28, 28, 1)\n",
    "\n",
    "# 將像素值由\"整數(0~255)\"換成\"浮點數(0.0~255.0)\"\n",
    "X_train = X_train.astype('float32')\n",
    "X_test = X_test.astype('float32')\n",
    "\n",
    "# 定義\"圖像數據增強產生器(ImageDataGenerator)\"的參數\n",
    "datagen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True)\n",
    "\n",
    "# 透過訓練數據集來訓練(fit)圖像數據增強產生器(ImageDataGenerator)的實例\n",
    "datagen.fit(X_train)\n",
    "\n",
    "# 設定要\"圖像數據增強產生器(ImageDataGenerator)\"產生的圖像批次值(batch size)\n",
    "# \"圖像數據增強產生器(ImageDataGenerator)\"會根據設定回傳指定批次量的新生成圖像數據\n",
    "for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9):\n",
    "    plt.figure(figsize=(8,8)) # 設定每個圖像顯示的大小\n",
    "    # 產生一個3x3網格的組合圖像\n",
    "    for i in range(0, 6):\n",
    "        plt.subplot(331+i)\n",
    "        plt.title(y_batch[i]) # 秀出圖像的真實值\n",
    "        plt.axis('off')     #不顯示坐標\n",
    "        plt.imshow(X_batch[i].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
    "        \n",
    "    plt.show()\n",
    "    break # 跳出迴圈"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "運行這個範例，您可以看到翻轉的數字。翻轉數字對於MNIST來說是沒有用的，因為它們總是具有正確的左右方向，但是這對於可能具有不同方向的場景中的對象的照片的問題可能是有用的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 影像特徵標準化 (Feature Standardization)\n",
    "\n",
    "我們可以把整個數據集中的每個像素值來進行標準化(standardize pixel)。這被稱為特徵標準化，你可以想像影像特徵標準化的程序如同我們對一般表格數據集中每個列會進次的資料標準化。\n",
    "\n",
    "您可以通過在ImageDataGenerator類別上設置featurewise_center和featurewise_std_normalization的參數來執行影像特徵標準化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFICAYAAACImy0WAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAGaxJREFUeJzt3XuQ1cWVwPHTwgBO8LWWmFAMjBKS\nQIyCPAahjGt0tVBYUGACWIlUYS3BXcJQQJ6aslwSnxRiWEhCDKFkZcAZAikjUkKMFV6GRBNSiCHI\n8iaBJMDyBuG3f5DUpvu0ub+53vs7d+58P1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n5855cewgjnB9tVA1sbqbb775/Qf7V2vXrs35OWmeJaLXgNNcKpEvi3lXqDl36623qtyrr76a8+ee\neuoplauvr/fi9957T9UcPXrUi2Pryz169PDi2NpuuE4ton+3Pv7446pmzpw5Xnzo0CFV0xyknXO8\nAQMAYIAGDACAARowAAAGaMAAABhoUZuwwgMtRPRGgNgtGmHummuuUTXhRqm0/78eP37ci3/zm9+o\nmqVLl3pxeBuIiMjBgwdTPc8Sm7D+X3jTUU1NjapJM6fS1DQ0NKhceENRmoMwYgeBhBt7Yod+xMYU\nHvxRW1uragp1OEdz3oQV2wQV3jQU3paWVuxgjj/96U8F+ezf//73Kvfkk0968fz58/P67OaATVgA\nAJQwGjAAAAZowAAAGKABAwBgoORuQyqmSy65ROUuvvhiL77qqqtyfs7OnTtVLtw8Fdt4snr1apVb\nuXKlF8dOuUL5CTdBvfDCC6omn1OuYrcRfelLX8pniMr69etVLtw8tWTJElUTG3e4CY3bkOJiJ0EN\nHTrUi7/4xS+qmmuvvVblBgwY4MVt2rRRNflsuopt8ps4caLKNYeNolnjDRgAAAM0YAAADNCAAQAw\n0KIO4oi5/PLLvbhVq1Y5f+b06dMqd+zYsYKNqVxxEAcsNOeDOPIV7m0R0XtgYoemDBo0yIvffvtt\nVbNjxw4vDg/vEImv+7ckHMQBAEAJowEDAGCABgwAgAEaMAAABlr8Jixkh01YsNASN2HBFpuwAAAo\nYTRgAAAM0IABADBAAwYAwAANGAAAAzRgAAAM0IABADBAAwYAwAANGAAAAzRgAAAM0IABADBAAwYA\nwAANGAAAAzRgAAAM0IABADBAAwYAwAANGAAAAy5JEusxAADQ4vAGDACAARowAAAGaMAAABigAQMA\nYIAGDACAARowAAAGaMAAABigAQMAYIAGDACAARowAAAGaMAAABigAQMAYIAGDACAARowAAAGaMAA\nABigAQMAYIAGDACAARowAAAGaMAAABigAQMAYIAGDACAARowAAAGaMAAABj4P53EXscWQsQ4AAAA\nAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1765e2d5748>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 對整個資料集進行\"影像特徵標準化\" , mean=0, stdev=1\n",
    "from keras.datasets import mnist\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 在keras的backend預設使用tensorflow\n",
    "# 而tensorflow在處理圖像是的資料結構是3維的張量 (圖像寬:width, 圖像高:height, 圖像頻道:channel)\n",
    "\n",
    "# 載入數據\n",
    "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
    "\n",
    "# 將圖像數據集的維度進行改變 \n",
    "# 改變前: [樣本數, 圖像寬, 圖像高] -> 改變後: [樣本數, 圖像寬, 圖像高, 圖像頻道數]\n",
    "X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\n",
    "X_test  = X_test.reshape(X_test.shape[0], 28, 28, 1)\n",
    "\n",
    "# 將像素值由\"整數(0~255)\"換成\"浮點數(0.0~255.0)\"\n",
    "X_train = X_train.astype('float32')\n",
    "X_test = X_test.astype('float32')\n",
    "\n",
    "# 定義\"圖像數據增強產生器(ImageDataGenerator)\"的參數\n",
    "datagen = ImageDataGenerator(featurewise_center=True, \n",
    "                             featurewise_std_normalization=True)\n",
    "\n",
    "# 透過訓練數據集來訓練(fit)圖像數據增強產生器(ImageDataGenerator)的實例\n",
    "datagen.fit(X_train)\n",
    "\n",
    "# 設定要\"圖像數據增強產生器(ImageDataGenerator)\"產生的圖像批次值(batch size)\n",
    "# \"圖像數據增強產生器(ImageDataGenerator)\"會根據設定回傳指定批次量的新生成圖像數據\n",
    "for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9):\n",
    "    plt.figure(figsize=(8,8)) # 設定每個圖像顯示的大小\n",
    "    # 產生一個3x3網格的組合圖像\n",
    "    for i in range(0, 6):\n",
    "        plt.subplot(331+i)\n",
    "        plt.title(y_batch[i]) # 秀出圖像的真實值\n",
    "        plt.axis('off')     #不顯示坐標\n",
    "        plt.imshow(X_batch[i].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
    "        \n",
    "    plt.show()\n",
    "    break # 跳出迴圈"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ZCA 白化轉換 (ZCA Whitening)\n",
    "\n",
    "圖像的白化變換是為了減少圖像像素矩陣中的冗餘/去除相關性(共線性)的線性特徵所進行的代數運算轉換。\n",
    "\n",
    "進行圖像中像素矩陣去除相關性的轉換是為了更好地突顯圖像中的結構和特徵到學習演算法。\n",
    "\n",
    "這個轉換的概念在以前大多是使用主成分分析（PCA）技術來執行圖像白化。最近，ZCA的技術顯示了更好的結果，對圖像進行ZCA轉換後的圖像可以保留原始尺寸的大小，從而使轉換後的圖像仍然看起來像原始圖像(這一點與PCA是不同的)。\n",
    "\n",
    "您可以通過將zca_whitening參數設置為True來執行ZCA白化轉換。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFICAYAAACImy0WAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJztnXvQXlV59u8tSCBHAiEhQDgGwiEQ\njoKAE1soRhGYftYRsfSz4NQho1I7tNqptjPMN/PVqa0jKGKHwRk5DhZQGdGvKoNAoQJykkMgQDiF\nhBxIQk6A4vP9gZ1xXeuK7+2T5F3P++b3m8kf95p777322muvlee9r33fXa/XCwAAABhe3tG6AwAA\nANsibMAAAAANYAMGAABoABswAABAA9iAAQAAGsAGDAAA0AA2YAAAgAawAQ8DXdft0nXdzV3Xre+6\n7vmu685p3ScY3TDnoAVd113ddd2Srute67ruqa7rPtG6T4PM9q07sI3w9Yh4MyKmRcSREfGDruse\n7vV6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syXivg1/AAAAADWAD\nBgAAaAAbMAAAQAPYgAEAABrQbYtCBwAAgNbwCxgAAKABbMAAAAANYAMGAABoABswAABAA9iAAQAA\nGsAGDAAA0AA2YAAAgAawAQMAADSADRgAAKABbMAAAAANYAMGAABoABvwMNB13dVd1y3puu61ruue\n6rruE637BKObrut26bru5q7r1ndd93zXdee07hOMfrqu27frulu7rlvVdd3Sruu+1nXd9q37Naiw\nAQ8P/zci9u31ehMj4syI+D9d1x3TuE8wuvl6RLwZEdMi4mMR8Y2u6w5r2yXYBrgsIpZFxPSIODIi\n5kbE/KY9GmDYgIeBXq/3WK/Xe+N/zN/+O6Bhl2AU03XduIj4UER8sdfrrev1endFxPcj4ty2PYNt\ngP0i4oZer/d6r9dbGhE/igj+47cJ2ICHia7rLuu6bkNELIiIJRFxa+MuwejloIh4q9frPfU7bQ8H\nCyFsfb4aEWd3XTe267o9I+L98fYmDAY24GGi1+vNj4gJEfGeiLgpIt74/UcA9M34iFgjbWvi7fkH\nsDX5Wbz9H73XIuKliLg/Ir7btEcDDBvwMNLr9d767Z8D94qIC1r3B0Yt6yJiorRNjIi1DfoC2whd\n170jIv5fvP0DY1xETImIyRHxpZb9GmTYgNuwfRADhq3HUxGxfdd1B/5O25yIeKxRf2DbYJeImBER\nX+v1em/0er2VEfGtiPhA224NLmzAW5mu66Z2XXd213Xju67bruu690XERyPittZ9g9FJr9dbH2//\nCrm467pxXdedFBFnRcRVbXsGo5ler7ciIhZFxAVd123fdd3OEfG/4239ARjYgLc+vXj7z80vRcSq\niPhyRPx1r9f7XtNewWhnfkTsFG9/EnJdRFzQ6/X4BQxbm/8VEfMiYnlEPB0Rv46Izzbt0QDT9Xq9\n1n0AAADY5uAXMAAAQAPYgAEAABrABgwAANAANmAAAIAGDGuVinPOOQfF1xbACee6rvuDfYaba6+9\ntkkHzjvvvFE77956663Cfuc731n5bLfddlXbm2++Wdit58bW5Morrxz2m5s/f/6onXNKVsg7mueY\nctlll6Vull/AAAAADWADBgAAa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+Zp57pnhK5jh3b5sTf2uNey76PrqEOuqTKSoQUY+VS2ikc1wTY0RE\nnHDCCYXtEs1897vfrdpuvPHGwv7IRz5S+ei80EI4ERHr1q0rbPfOOTLFPXRtc3NuSxb84BcwAABA\nA9iAAQAAGsAGDAAA0AA2YAAAgAYMnAhrSyV0cIHybFs/fcoE+B16/UxCgkwiDHd9J6rR45woSwUd\nI11wlSWTeEJFGlmhm457Zh5mhH3Z5ALa78z1neApk1DDJYrQe8nMe3d9NyYjGb0fN3Y6Ls7HzTmd\nG06oqdd3iUB07rg556oRab9feOGFymeoYyLq9ciJIzO4d1Xn2NZOOsQvYAAAgAawAQMAADSADRgA\nAKABAxcDdvQTL3O4v/lnYrBDHeOOy8YOtlQMWs+TTfqh8aPRFlPbkrjnoOPl4lGZpDDug/8MLv6n\nuLmgcbuMZiBzHkemMADkycRgM4ll3NzJrBsaJ3XXyiaEUTIFOPT6rs8uEYlqOjJzfmsXmeEXMAAA\nQAPYgAEAABrABgwAANAANmAAAIAGdFuysgMAAADk4BcwAABAA9iAAQAAGsAGDAAA0AA2YAAAgAaw\nAQMAADSADRgAAKABbMAAAAANYAMGAABoABswAABAA9iAAQAAGsAGDAAA0AA2YAAAgAawAQMAADSA\nDRgAAKABbMAAAAANYAMGAABoABswAABAA9iAAQAAGsAGDAAA0AA2YAAAgAawAQMAADSADRgAAKAB\nbMAAAAAN+P87JsgRcvP0fQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1765c068860>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# ZCA 白化轉換 (ZCA Whitening)\n",
    "from keras.datasets import mnist\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 載入數據\n",
    "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
    "\n",
    "# 將圖像數據集的維度進行改變 \n",
    "# 改變前: [樣本數, 圖像寬, 圖像高] -> 改變後: [樣本數, 圖像寬, 圖像高, 圖像頻道數]\n",
    "X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\n",
    "X_test  = X_test.reshape(X_test.shape[0], 28, 28, 1)\n",
    "\n",
    "# 將像素值由\"整數(0~255)\"換成\"浮點數(0.0~255.0)\"\n",
    "X_train = X_train.astype('float32')\n",
    "X_test = X_test.astype('float32')\n",
    "\n",
    "# 定義\"圖像數據增強產生器(ImageDataGenerator)\"的參數\n",
    "datagen = ImageDataGenerator(zca_whitening=True)\n",
    "\n",
    "# 透過訓練數據集來訓練(fit)圖像數據增強產生器(ImageDataGenerator)的實例\n",
    "datagen.fit(X_train)\n",
    "\n",
    "# 設定要\"圖像數據增強產生器(ImageDataGenerator)\"產生的圖像批次值(batch size)\n",
    "# \"圖像數據增強產生器(ImageDataGenerator)\"會根據設定回傳指定批次量的新生成圖像數據\n",
    "for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9):\n",
    "    plt.figure(figsize=(8,8)) # 設定每個圖像顯示的大小\n",
    "    # 產生一個3x3網格的組合圖像\n",
    "    for i in range(0, 6):\n",
    "        plt.subplot(331+i)\n",
    "        plt.title(y_batch[i]) # 秀出圖像的真實值\n",
    "        plt.axis('off')     #不顯示坐標\n",
    "        plt.imshow(X_batch[i].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
    "        \n",
    "    plt.show()\n",
    "    break # 跳出迴圈"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "運行以上範例，您可以在圖像中看到相同的一般數字結構以及每個數字的輪廓如何被突顯。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 儲存增強的圖像數據　(Saving Augmented Images to File)\n",
    "\n",
    "數據準備和增強由Keras在模型訓練的運行中進行。\n",
    "\n",
    "這對記憶體的使用效率很高，但是您有可能需要在訓練期間使用在檔案系統中圖像檔。例如，也許您希望稍後使用不同的套件　進行分析，或者只生成一次新增強的圖像數據，然後在多種不同的深度學習模型或配置中使用它們。\n",
    "\n",
    "Keras允許您保存訓練期間動態生成的圖像。您可以在訓練前指定給flow（）函數有關於您想保存的目錄、文件名前綴和圖像文件類型。然後在訓練過程中，動態生成的圖像將被寫入到檔案系統中。\n",
    "\n",
    "下面的範例演示了這一點，並將6個圖像寫入“images”子目錄，前綴為“aug”，文件類型為PNG。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFICAYAAACImy0WAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAG/JJREFUeJzt3XmQVdXxwPE+wJAwjAMSICwKJPkp\nKgGBYIgrUOwgCrJKZCuBkpCIpShRAQNoUKmAJMEoCorBlKIIJVtEMJQLEEAigSihABEQkX3fZuD+\n/iBWcm5fnDeP916/ee/7qfpVft3Vc2+XXqe93sM5LggCAQAAqVXKugEAALIRAxgAAAMMYAAADDCA\nAQAwwAAGAMAAAxgAAAMMYAAADDCAU8g5d4Vz7pRzbqZ1L8gOzrlezrnPnHPHnXNbnHM3W/eEzMfv\nutiUsW4gy0wRkdXWTSA7OOdai8hTItJTRFaJSHXbjpBF+F0XA96AU8Q510tEDonIUutekDXGiMjY\nIAhWBkFwLgiCL4Mg+NK6KWQ2ftfFjgGcAs65fBEZKyIPWPeC7OCcKy0iTUSkinNus3Nup3Puj865\ncta9IXPxu654GMCpMU5EpgVBsMO6EWSN74tIjoh0E5GbRaShiDQSkZGWTSHj8buuGBjASeacaygi\nrURkknUvyCon//O/fwiC4KsgCPaJyEQR6WDYEzIYv+uKj0VYyddcROqIyHbnnIhInoiUds5dEwRB\nY8O+kMGCIDjonNspIhx3hlRpLvyuKxbHcYTJ5ZzLFZH8/0kNl/MP6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DAgCc17Rp07h+rnfv3gnuJHvxBgwAgAEGMAAA\nBhjAAAAYYCMOpAwbccACG3Eg1diIAwCANMYABgDAAAMYAAADDGAAAAykdBEWAAA4jzdgAAAMMIAB\nADDAAAYAwAADGAAAAwxgAAAMMIABADDAAAYAwAADGAAAAwxgAAAMMIABADDAAAYAwAADGAAAAwxg\nAAAMMIABADDAAAYAwAADGAAAAwxgAAAMMIABADDAAAYAwAADGAAAAwxgAAAMMIABADDAAAYAwMD/\nA57vAEYKKvWAAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1765e2f0c18>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 儲存增強的圖像數據　(Saving Augmented Images to File)\n",
    "from keras.datasets import mnist\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "\n",
    "# 載入數據\n",
    "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
    "\n",
    "# 將圖像數據集的維度進行改變 \n",
    "# 改變前: [樣本數, 圖像寬, 圖像高] -> 改變後: [樣本數, 圖像寬, 圖像高, 圖像頻道數]\n",
    "X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\n",
    "X_test  = X_test.reshape(X_test.shape[0], 28, 28, 1)\n",
    "\n",
    "# 將像素值由\"整數(0~255)\"換成\"浮點數(0.0~255.0)\"\n",
    "X_train = X_train.astype('float32')\n",
    "X_test = X_test.astype('float32')\n",
    "\n",
    "# 定義\"圖像數據增強產生器(ImageDataGenerator)\"的參數\n",
    "datagen = ImageDataGenerator()\n",
    "\n",
    "# 透過訓練數據集來訓練(fit)圖像數據增強產生器(ImageDataGenerator)的實例\n",
    "datagen.fit(X_train)\n",
    "\n",
    "# 產生要保存圖像檔案的目錄\n",
    "if not os.path.exists('images'):\n",
    "    os.makedirs('images')\n",
    "    \n",
    "# 設定要\"圖像數據增強產生器(ImageDataGenerator)\"產生的圖像批次值(batch size)\n",
    "# \"圖像數據增強產生器(ImageDataGenerator)\"會根據設定回傳指定批次量的新生成圖像數據\n",
    "for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9, save_to_dir='images', save_prefix='aug', save_format='png'):\n",
    "    plt.figure(figsize=(8,8)) # 設定每個圖像顯示的大小\n",
    "    # 產生一個3x3網格的組合圖像\n",
    "    for i in range(0, 6):\n",
    "        plt.subplot(331+i)\n",
    "        plt.title(y_batch[i]) # 秀出圖像的真實值\n",
    "        plt.axis('off')     #不顯示坐標\n",
    "        plt.imshow(X_batch[i].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
    "        \n",
    "    plt.show()\n",
    "    break # 跳出迴圈"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "運行範例，您可以看到只有在圖像生成時才會也把寫入圖像寫到檔案系統中。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用Keras增強圖像數據的技巧"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "圖像數據很獨特，因為您可以查看數據和數據的轉換副本，並快速了解模型如何通過您的圖像感知到某種影像特徵。\n",
    "\n",
    "\n",
    "以下是從圖像數據準備和增強深度學習中獲得最多的一些時間。\n",
    "\n",
    "* **查看圖像數據集** 花一些時間仔細檢查你的數據集。看圖像。注意可能有利於模型訓練過程的圖像準備和增強，例如需要處理場景中不同的輪班，旋轉或翻轉對象。\n",
    "* **查看增強過的圖像** 增強操作完成後，查看一些範例圖像。理智地知道你正在使用什麼樣的圖像變換是一回事，真正看到範例圖像是非常不同的事情。查看您正在使用的單個增強圖像以及您計劃使用的全套增強圖像。您可能會看到簡化或進一步增強您的模型訓練的方法。\n",
    "* **評估一整套變換** 嘗試多個圖像數據準備和增強方案。通常情況下，您認為這樣做不會有好處的轉換, 最後您可能會對數據增強後對模型的結果感到驚訝。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 總結\n",
    "在這篇文章中，您瞭解了圖像數據準備和增強的一些面向。\n",
    "\n",
    "您發現了一系列可以輕鬆使用Keras來使用圖像數據增強(data augmentation)應用在深度學習模型的技術。您了解到：\n",
    "* Keras中的ImageDataGenerator API可以用來動態產生新的轉換後的圖像來用於訓練。\n",
    "* 圖像像素的標準化手法。\n",
    "* ZCA白化轉換。\n",
    "* 對圖像進行隨機旋轉，移動和翻轉。\n",
    "* 如何將轉換後的圖像保存到檔案系統中以備後用。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "參考:\n",
    "* [Keras官網](http://keras.io/)\n",
    "* [Image Augmentation for Deep Learning With Keras](https://machinelearningmastery.com/image-augmentation-deep-learning-keras/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
